#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Zhangcl
import time
from db.connect_db import engine,Students,Teacher,Classes,Class_choise,Class_record,Homework,Grade,Admin
from sqlalchemy.orm import sessionmaker
from sqlalchemy import and_, or_
session_connet = sessionmaker(bind=engine)
session = session_connet()
class Teacher_run(object):
    def __init__(self,user):
        self.user = user
        obj = session.query(Teacher).filter(Teacher.name == self.user).first()
        self.id =obj.id
    def menu(self):
        choise_menu = '''
        \033[1;31m
           ---------Welcome  %s----------
           1. 创建班级
           2. 添加学生
           3. 创建上课记录
           4. 修改成绩
           5. 退出
        \033[0m

        '''%self.user

        menu_dict ={
            '1':Teacher_run.create_class,
            '2':Teacher_run.add_student,
            '3':Teacher_run.create_record,
            '4':Teacher_run.update_grade,
            '5':'exit'
        }

        while True:
            print(choise_menu)
            choise = input('请选择>>>')
            if choise in menu_dict:
                if choise == '5':
                    exit()
                else:
                    menu_dict[choise](self)
            else:
                print('输入错误！')

    def create_class(self):
        '''
        创建课程模块
        :return:
        '''
        class_name = input('请输入课程名>>>')
        teacher_id = self.id
        obj_add = Classes(name=class_name,teacher_id=teacher_id)#创建班级，并绑定教师ID
        session.add(obj_add)
        session.commit()
        print('创建成功！')
    def add_student(self):
        '''
        添加学生模块
        :return:
        '''
        obj = session.query(Classes.id,Classes.name).filter(Classes.teacher_id == self.id).all()
        for index,item in enumerate(obj):
            print(index,item[-1]) #查询该教师ID关联的班级
        class_choise =input('请选择班级>>>')
        if class_choise.isdigit():
            if obj[int(class_choise)]:
                class_id = obj[int(class_choise)][0] #获取课程ID
                student_qq = input('请输入学生qq>>>')
                obj_student=session.query(Students.qq).filter(Students.qq == student_qq).first()
                if obj_student: #先查询该学生是否存在
                    obj_class_choise = session.query(Class_choise).filter(and_(Class_choise.student_qq==student_qq,
                                                                          Class_choise.class_id==class_id)).first()
                    if obj_class_choise is None:#再查询该课程学生是否已绑定，返回空为未绑定
                        obj_add = Class_choise(class_id=class_id,student_qq=student_qq)
                        session.add(obj_add)
                        session.commit()
                        print('创建成功！')
                    else:
                        print('已存在！')
                else:
                    print('该学生不存在！')
            else:
                print('输入错误！')
        else:
            print('输入错误！')

    def create_record(self):
        '''
        创建上课记录
        :return:
        '''
        obj = session.query(Classes.id, Classes.name).filter(Classes.teacher_id == self.id).all()
        for index, item in enumerate(obj):
            print(index, item[-1]) #查询该教师ID关联的班级
        class_choise = input('请选择班级>>>')
        if class_choise.isdigit():
            if obj[int(class_choise)]:
                class_id = obj[int(class_choise)][0]#获取课程ID
                local_date = time.strftime('%Y-%m-%d')#根据日期自动生成当天的课程记录
                obj_date = session.query(Class_record).filter(and_(Class_record.date == local_date,
                                                                   Class_record.class_id==class_id)).first()
                if obj_date is None:#查询该课程当天是否已经生成的上课记录，空为未创建
                    obj_student = session.query(Class_choise.student_qq).filter(Class_choise.class_id == class_id).all()
                    for i in obj_student:
                        record_add = Class_record(class_id=class_id,date=local_date,teacher_id=self.id,student_qq=i[0])
                        session.add(record_add)
                        homework_add = Homework(class_id=class_id,date=local_date,student_qq=i[0],status='NO')
                        session.add(homework_add)
                        grade_add = Grade(class_id=class_id,date=local_date,student_qq=i[0],grade=0,status='NO')
                        session.add(grade_add)
                    session.commit()
                    print('创建成功！')
                else:
                    print('该课程今日上课记录已存在！')
            else:
                print('输入错误！')
    def update_grade(self):
        '''
        更新成绩模块
        :return:
        '''
        obj_class =session.query(Classes).filter(Classes.teacher_id == self.id).all()#查询该教师绑定的课程
        for index,i in enumerate(obj_class):
            print(index,i.name)
        class_choise = input('请选择班级>>>')
        if class_choise.isdigit():
            if obj_class[int(class_choise)]:
                class_id = obj_class[int(class_choise)].id#获取需要的课程id
                obj_grade = session.query(Grade.date).filter(Grade.class_id==class_id).group_by(Grade.date).all()#获取该课程的上课记录
                for index,f in enumerate(obj_grade):
                    print(index,f.date)
                date_choise = input('请选择上课日期>>>')
                if date_choise.isdigit():
                    if obj_grade[int(date_choise)]:
                        date = obj_grade[int(date_choise)].date#获取需要的上课记录
                        student_find = session.query(Grade).filter(and_(Grade.class_id==class_id,
                                                                      Grade.date==date,
                                                                      Grade.status=='YES')).all()#查询对应课程对应上课记录下，已提交作业的学生名单，未提交作业的名单不展示！

                        for index,s in enumerate(student_find):
                            print(index,s.student.name)
                        student_choise = input('请选择学生>>>')
                        if student_choise.isdigit():
                            if student_find[int(student_choise)]:
                                student_qq=student_find[int(student_choise)].student_qq #选择学生
                                update_grade=input('请输入成绩>>>')
                                if update_grade.isdigit():
                                    if int(update_grade)>=0 or int(update_grade)<=100:
                                        obj_grade=session.query(Grade).filter(and_(Grade.class_id==class_id,
                                                                                   Grade.date==date,
                                                                                   Grade.status=='YES',
                                                                                   Grade.student_qq==student_qq)).first()#获取需要修改成绩的准确信息条目
                                        obj_grade.grade=update_grade #修改成绩
                                        session.commit()
                                        print('修改成功！')
                                    else:
                                        print('输入错误！')
